<!-- TMS 回单管理 -->
<template>
  <div class="page-container">
    <page_breadcrumb :getKeyMethod="init_table_model" @change="change_table_model" />

    <ex_super_table_page v-bind="option" ref="SuperTablePage" @selection-change="e => selection = e">
    </ex_super_table_page>

    <!-- 回单上传 查看 -->
    <receipt_upload ref="receiptUploadRef" @submitSuccess="reload()"></receipt_upload>
    <!-- 回单回收 -->
    <receipt_recycle ref="receiptRecycleRef" @submitSuccess="reload()"></receipt_recycle>
    <!-- 回单交接 -->
    <receipt_handover ref="receiptHandoverRef" @submitSuccess="reload()"></receipt_handover>
    <!-- 签收确认 -->
    <receipt_confirmation ref="receiptConfirmationRef" @submitSuccess="reload()"></receipt_confirmation>

  </div>
</template>

<script>
import superTablePageFunction from '@/mixin/super-table-page-function';
import page_table_model_manage from '@/mixin/page_table_model_manage';
import router_params from '@/mixin/router_params';

// 回单上传 查看
import receipt_upload from './dialog/receipt_upload.vue';
// 回单回收
import receipt_recycle from './dialog/receipt_recycle.vue';
// 回单交接
import receipt_handover from './dialog/receipt_handover.vue';
// 签收确认
import receipt_confirmation from './dialog/receipt_confirmation.vue';

import emitOnFunction from "@/mixin/emit-on-function";
let emitKey = "receipt_manage>confirm";
export default {
  title: "label_receipt_manage",
  components:{receipt_upload,receipt_recycle,receipt_handover,receipt_confirmation},
  mixins: [superTablePageFunction, page_table_model_manage, router_params, emitOnFunction(emitKey),],
  data() {
    return {
      selection:[],
      option: {
        //搜索框内容
        search: {
          //搜索框内容，key为搜索字段，label为标签提示
          model: [
            { key: 'orderCode', label: 'label_tms_receipt_order_platform_number' },
            { key: 'customerName', label: 'label_customer_name' },
            // { key: 'waybillTaskCode', label: 'label_tms_receipt_task_number'},
            { key: 'tmsReceiptStatusEnum', label: 'label_receipt_order_status',type: 'select', enum: 'TmsReceiptStatusEnum' },

            { key: 'receiptUserName', label: 'label_tms_track_consignee', fold: true  },
            { key: 'trackNumber', label: 'label_tms_track_task_number', fold: true },
            {
              key: 'uploadTime',
              label: 'label_tms_receipt_receipt_upload_date',
              type: 'date-picker',
              fold: true
            },
            {
              key: 'handoverTime',
              label: 'label_tms_receipt_receipt_handover_time',
              type: 'date-picker',
              fold: true
            },
          ],
        },
        actionExtra: [
          { label: 'label_tms_receipt_receipt_recycle', attrs: { type: "primary" }, click: ()=>this.receiptRecycleFn() },
          { label: 'label_tms_receipt_receipt_handover', attrs: { type: "primary" }, click: ()=>this.receiptHandoverFn() },
          { label: 'label_tms_receipt_receipt_confirmation', attrs: { type: "primary" }, click: ()=>this.signFn() },
          { label: 'label_export', attrs: { type: "primary" }, click: this.exportExcel }
        ],
        table: {
          url: "/tms-api/task/TmsOrderReceiptInfoRpc/selectByPage.json",
          // urlParams: (params) => {
          //   return params
          // },
          model: this.init_table_model(),
        }
      },
    };
  },
  methods: {
    option_table_model() {
      return [
        { type: "selection", clickToggle: true },
        { key: 'orderCode', label: 'label_tms_receipt_order_platform_number', 'show-overflow-tooltip': true, },
        { key: 'isReceipt', label: 'label_tms_receipt_signing_service', 'show-overflow-tooltip': true,formatter: (r, c, v) => this.$enumShow(v, "YesOrNoEnum") },
        // { key: '', label: 'label_tms_receipt_task_number', 'show-overflow-tooltip': true, },
        { key: 'trackNumber', label: 'label_tms_track_task_number', 'show-overflow-tooltip': true, },
        { key: 'customerName', label: 'label_customer_name', 'show-overflow-tooltip': true, },
        { key: '', label: 'label_tms_receipt_signing_condition', 'show-overflow-tooltip': true,formatter: (r, c, v) => this.$enumShow(v, "YesOrNoEnum") },
        { key: 'receiptUserName', label: 'label_tms_track_consignee', 'show-overflow-tooltip': true, },
        { key: 'deliveryCityName', label: 'label_tms_receipt_commence_address', 'show-overflow-tooltip': true, },
        { key: 'receiptCityName', label: 'label_tms_receipt_destination', 'show-overflow-tooltip': true, },
        { key: 'trackTaskStatusEnum', label: 'label_order_status', 'show-overflow-tooltip': true,formatter: (r, c, v) => this.$enumShow(v, "TrackTaskStatusEnum") },
        { key: 'tmsReceiptStatusEnum', label: 'label_receipt_order_status', 'show-overflow-tooltip': true, formatter: (r, c, v) => this.$enumShow(v, "TmsReceiptStatusEnum")},
        { key: 'uploadTime', label: 'label_tms_receipt_receipt_upload_date', 'show-overflow-tooltip': true,formatter: (r, c, v, i) => this.$dateFormat(v) },
        {
          key: 'handoverTime',
          label: 'label_tms_receipt_receipt_handover_time',
          formatter: (r, c, v) => this.$dateFormat(v)
        },
        { key: 'tmsReceiptHandoverEnum', label: 'label_receipt_handover_type', 'show-overflow-tooltip': true, formatter: (r, c, v) => this.$enumShow(v, "TmsReceiptHandoverEnum")},

        {
          type: 'operate', width: 210, operate: [

            {
              type: 'more',
              items: [
                {
                  label: "label_tms_receipt_receipt_view",
                  disabled:row=>row.tmsReceiptStatusEnum==='WAIT_UPLOAD',
                  click: (row) => this.receiptUploadFn(row,true)
                },
                {
                  label: "label_tms_receipt_receipt_down",
                  disabled:row=>row.tmsReceiptStatusEnum==='WAIT_UPLOAD',
                  click: (row) => this.receiptDowndFn(row)
                },
                {
                  label: "label_tms_receipt_receipt_upload",
                  disabled:row=>!['WAIT_UPLOAD','WAIT_RECOVERY'].includes(row.tmsReceiptStatusEnum),
                  click: (row) => this.receiptUploadFn(row,false)
                },
                {
                  label: "label_tms_receipt_receipt_recycle",
                  disabled:row=>row.tmsReceiptStatusEnum!=='WAIT_RECOVERY',
                  click: (row) => this.receiptRecycleFn(row)
                },
                {
                  label: "label_tms_receipt_receipt_handover",
                  disabled:row=>row.tmsReceiptStatusEnum!=='WAIT_HANDOVER',
                  click: (row) => this.receiptHandoverFn(row)
                },
                {
                  label: "label_tms_receipt_receipt_confirmation",
                  disabled:row=>row.tmsReceiptStatusEnum!=='WAIT_SIGN',
                  click: (row) => this.signFn(row)
                },
                {
                  label: "label_view_detail",
                  click: (row) => {
                    this.$router.push({ path: '/page/tms/receipt/receipt_detail', query: { id: row.id, } })
                  }
                },
              ]
            }
          ]
        }
      ]
    },
    receiptDowndFn(row){
      this.$ajax({
          url: "/tms-api/task/TmsOrderReceiptInfoRpc/receiptView.json",
          method: 'post',
          // type: 'json',
          data: {
            receiptId:row.id
          },
          success: ({data}) => {
            let a = document.createElement('a')
            a.href = this.$ex_default_download_url+data
            a.download = 'pic'
            a.click()

          }
      })
    },
    exportExcel() {
      this.$downloadFile({
        loading: true,
        type: 'json',
        url: '/tms-api/task/TmsOrderReceiptInfoRpc/exportExcel.json',
        data: {
          receiptIdList: this.selection.map(it => it.id),
          ...this.getSuperTablePage().getSearchFormData()
        },
      })
    },
    receiptUploadFn(row,readonly=false){
     if(['WAIT_UPLOAD','WAIT_RECOVERY'].includes(row.tmsReceiptStatusEnum)||readonly){
      this.$refs.receiptUploadRef.show(row,readonly)
     }else{
        this.$message.error(this.$t('label_receipt_tip_submit_check_WAIT_WAIT_UPLOAD'))
        return
     }

    },
    // 签收
    signFn(row){
      let rows = this.selection
      if(row){
        rows = [row]
      }else if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      const WAIT_SIGN =  rows.every(item=>item.tmsReceiptStatusEnum==='WAIT_SIGN')
      if(!WAIT_SIGN){
        this.$message.error(this.$t('label_receipt_tip_submit_check_WAIT_WAIT_SIGN'))
        return
      }

      const customer =  rows.every(item=>item.customerId===rows[0].customerId)
      if(!customer){
        this.$message.error(this.$t('label_receipt_tip_submit_check_customer'))
        return
      }

      const tmsReceiptHandoverEnum =  rows.every(item=>item.tmsReceiptHandoverEnum===rows[0].tmsReceiptHandoverEnum)
      if(!tmsReceiptHandoverEnum){
        this.$message.error(this.$t('label_receipt_tip_submit_check_tmsReceiptHandover'))
        return
      }
      if(rows[0].tmsReceiptHandoverEnum === 'FACE_TO_FACE'){
        const handoverUsername =  rows.every(item=>item.handoverUsername===rows[0].handoverUsername)
        if(!handoverUsername){
          this.$message.error(this.$t('label_receipt_tip_submit_check_handoverUsername'))
          return
        }
      }else{
        const handoverExpressNo  =  rows.every(item=>item.handoverExpressNo ===rows[0].handoverExpressNo )
        if(!handoverExpressNo ){
          this.$message.error(this.$t('label_receipt_tip_submit_check_handoverExpressNo'))
          return
        }
      }
      // 预约需要显示时间
      this.$refs.receiptConfirmationRef.show(rows,true)
    },
    //回单回收
    receiptRecycleFn(row){
      let rows = this.selection
      if(row){
        rows = [row]
      }else if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }

      const WAIT_RECOVERY =  rows.every(item=>item.tmsReceiptStatusEnum==='WAIT_RECOVERY')
      if(!WAIT_RECOVERY){
        this.$message.error(this.$t('label_receipt_tip_submit_check_WAIT_RECOVERY'))
        return
      }

      const every =  rows.every(item=>item.customerId===rows[0].customerId)
      if(!every){
        this.$message.error(this.$t('label_receipt_tip_submit_check_customer'))
        return
      }
      this.$refs.receiptRecycleRef.show(rows,true)
    },
    //回单交接
    receiptHandoverFn(row){
      let rows = this.selection
      if(row){
        rows = [row]
      }else if (_.isEmpty(this.selection)) {
        return this.$message.error(this.$tt('tip_please_select_order'))
      }
      const WAIT_HANDOVER =  rows.every(item=>item.tmsReceiptStatusEnum==='WAIT_HANDOVER')
      console.log(WAIT_HANDOVER)
      if(!WAIT_HANDOVER){
        this.$message.error(this.$t('label_receipt_tip_submit_check_WAIT_HANDOVER'))
        return
      }

      const every =  rows.every(item=>item.customerId===rows[0].customerId)
      if(!every){
        this.$message.error(this.$t('label_receipt_tip_submit_check_customer'))
        return
      }
      this.$refs.receiptHandoverRef.show(rows,true)
    }
  }
}
</script>
<style scoped></style>
